package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.User;
import com.wms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2024-11-02
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public Result  list(){
        List<User> list = userService.list();
        Integer total = userService.count();
        return Result.success(list,total);
    }


    // 增加用户
    @PostMapping("/add")
    public boolean add(@RequestBody User user) {
        return userService.save(user);
    }

    // 更新用户
    @PutMapping("/update")
    public boolean update(@RequestBody User user) {
        return userService.updateById(user);
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Long id) {
        return userService.removeById(id);
    }

    // 模糊查询用户
    @PostMapping("/search")
    public List<User> search(@RequestParam String name) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName, name); // 假设User类中有name字段
        return userService.list(queryWrapper);
    }

    @PostMapping("/page")
    public IPage<User> getUserPage(@RequestBody QueryPageParam queryPageParam) {
        Page<User> page = new Page<>(queryPageParam.getCurrentPage(), queryPageParam.getFy());
        return userService.page(page, null); // null 为查询条件
    }


}
